package com.ride.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.ride.dao.ProfileDao;
import com.ride.model.Operator;
import com.ride.model.Profile;
import org.springframework.stereotype.Repository;

/**
 * @version $LastChangedRevision: 16 $
 *          <p/>
 *          $LastChangedBy: marcel.menze@gmail.com $
 *          <p/>
 *          $LastChangedDate: 2011-04-05 12:34:26 +0000 (Tue, 05 Apr 2011) $
 */
@Repository
public class ProfileDaoImpl implements ProfileDao {

    @PersistenceContext(name = "ride")
    private EntityManager em;

    /**
     * {@inheritDoc}
     */
    public Profile findById(Long id) {
        return em.find(Profile.class, id);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void persist(Profile profile) {
        em.persist(profile);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void update(Profile profile) {
        em.merge(profile);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void delete(Profile profile) {
        em.remove(profile);
    }

    /**
     * {@inheritDoc}
     */
    public List<Operator> findByFirstnameAndLastname(String firstName, String lastName) {
        List<Operator> operators = em.createQuery("select p from Profile p where p.firstname =:firstName and p.lastname = :lastName").
                setParameter("firstName", firstName).
                setParameter("lastName", lastName).getResultList();
        return operators;
    }
}
